package main

import (
	"github.com/CyCoreSystems/agi"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"log"
	"sync"
)

type Report struct {
	Type         string `bson:"type"`
	Account      string `bson:"account"`
	ReportStatus string `bson:"reportStatus"`
	ReportTime   string `bson:"reportTime"`
	BusinessData interface{}
}

func Reporta() (*mgo.Collection, *mgo.Session) {
	session, err := mgo.Dial("192.168.121.61:7290")
	if err != nil {
		panic(err)
	}
	//defer session.Close()
	// Optional. Switch the session to a monotonic behavior.
	session.SetMode(mgo.Monotonic, true)
	c := session.DB("report").C("bill_report_event")
	return c, session
}

var wg sync.WaitGroup

func main() {
	var result []Report
	wg.Add(20)

	for i := 0; i < 20; i++ {
		go func(i int) {
			log.Println("start work", i)
			defer wg.Done()
			C, S := Reporta()
			defer S.Close()
			err := C.Find(bson.M{}).All(&result)
			if err != nil {
				panic(err)
			}
			//for i, m := range result {
			//	fmt.Println(i, m)
			//}
			log.Println("success work", i)
		}(i)
	}

	wg.Wait()
}
